package com.face.console.service.impl;

import com.face.console.dao.MenuDtoMapper;
import com.face.console.pojo.dto.MenuDto;
import com.face.console.service.MenuService;
import com.face.console.tools.Error;
import com.face.console.tools.R;
import com.face.console.tools.SnowFlakeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Description:
 * <p>
 * Created by Andy_Lau on 2020/12/15 18:26
 */
@Service
public class MenuImpl implements MenuService
{

    public static final String CACHE_NAME = "local_";

    @Autowired
    private  MenuDtoMapper menuDtoMapper;


    @Value("${spring.redis.enable}")
    private boolean localEnable;

    @Override
//    @CachePut(value = CACHE_NAME, key = "'key_'+#productInfo.getId()") 保存到本地缓存中
    @Cacheable(value = CACHE_NAME+"queryMenuList")//新增本地查詢缓存注解
//    @CacheEvict(value = "queryMenuList")//刪除本地缓存注解
    public List<MenuDto> queryMenuList() {
        if (localEnable) {
            //缓存是否开启
            System.out.println("localEnablelocalEnable");
        }
        return menuDtoMapper.queryAllMenus();
    }

    @Override
    public List<MenuDto> querypidMenuById(Long id) {
        List<MenuDto> nodeList= listAllMenu(id);
        return nodeList;
    }

    @Override
    public R saveMenu(MenuDto param) {
        if (param == null) {
            return R.error(Error.参数为空异常);
        }
        if (param.getId()==null){
            param.setId(SnowFlakeUtil.getFlowIdInstance().nextId());
            menuDtoMapper.insertSelective(param);
            return R.success();
        }else{
            menuDtoMapper.updateByPrimaryKeySelective(param);
            return R.success();
        }

    }


    public  List<MenuDto> listAllMenu(Long id) {
        List<MenuDto> node = menuDtoMapper.queryMenuList(id);
        for (MenuDto dto : node) {
            // 判断是不是菜单
            if (dto.getPid().equals(id)) {
                //递归调用自己
                System.out.println("菜单名=" + dto.getMenuName());
                dto.setMenuChildNode(listAllMenu(dto.getId()));
            } else {
                System.out.println("底节点=");
            }
        }
        return node;
    }

}
